package com.czz.portal.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.portal.entity.Question;
import com.czz.portal.entity.QuestionComment;
import com.czz.portal.mapper.QuestionCommentMapper;
import com.czz.portal.mapper.QuestionMapper;
import com.czz.portal.service.QuestionCommentService;

import com.czz.portal.vo.CommentNestedVo;
import com.czz.portal.vo.UserQuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @ClassName: QuestionCommentServiceImpl
 * @Author Czz
 * @Date 2020/2/14
 * @Time 17:13
 **/
@Service
public class QuestionCommentServiceImpl extends ServiceImpl<QuestionCommentMapper, QuestionComment> implements QuestionCommentService {

    @Autowired
    QuestionCommentMapper commentMapper;

    @Autowired
    QuestionMapper questionMapper;


    @Override
    public List<CommentNestedVo> getCommentNestedVoPageList(Page<CommentNestedVo> pageParam, String id) {
        return commentMapper.getCommentNestedVoPageList(pageParam,id);
    }

    @Override
    @Transactional
    public void saveComment(QuestionComment comment) {
        //存入数据库
        commentMapper.insert(comment);
        //如果为二级评论，增加一级评论回复数

        if (!comment.getParentId().equals("0")){
            commentMapper.commentReplyPlus(comment.getParentId());
        }
        //如果为一级评论，增加提问评论数
        if (comment.getParentId().equals("0")){
            System.out.println(comment.getQuestionId());
            questionMapper.questionReplyPlus(comment.getQuestionId());
        }
    }

    @Override
    public List<UserQuestionVo> getUserCommentPageList(Page<UserQuestionVo> pageParam, String userId) {
        return commentMapper.getUserCommentPageList(pageParam,userId);
    }

    @Override
    public void updateCommentBest(QuestionComment questionComment) {
        //回答用户id
        String userId = questionComment.getUserId();
        String commentId = questionComment.getCommentId();

        QuestionComment comment = commentMapper.selectById(commentId);
        String questionId = comment.getQuestionId();

        //判断提问者id是否一致
        Question question = questionMapper.selectById(questionId);

        //修改回答为最佳回答，并修改问答状态为1
        if (!question.getUserId().equals(userId)){
            comment.setBest(1);
            commentMapper.updateById(comment);
            question.setStatus(1);
            questionMapper.updateById(question);
        }
    }
}
